package org.dragonnova.meetingserver.server.adapter;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import org.dragonnova.meetingserver.model.bean.VoiceInfo;
import org.dragonnova.meetingserver.server.activity.MainActivity;
import org.dragonnova.meetingserver.R;
import org.dragonnova.meetingserver.server.service.TranslationService;
import org.dragonnova.meetingserver.utils.TimeUtil;

import java.io.File;
import java.util.ArrayList;

/**
 * Created by hucn on 2015/12/22.
 * Description:聊天页面的聊天item的适配器
 */
public class ChatListAdapter extends BaseAdapter {

    private MainActivity mContext;
    private ArrayList<VoiceInfo> chatList;
    private TranslationService.MyBinder mMyBinder;

    /**
     * 有两种不同的item
     */
    private final int CHINESE_TO_ENGLISH = 0;
    private final int ENGLISH_TO_CHINESE = 1;

    public ChatListAdapter(MainActivity context, ArrayList<VoiceInfo> chatList, TranslationService.MyBinder myBinder) {
        this.chatList = chatList;
        this.mContext = context;
        this.mMyBinder = myBinder;
    }


    @Override
    public int getCount() {
        return chatList.size();
    }

    @Override
    public Object getItem(int position) {
        return chatList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        char isCH2EN = chatList.get(position).getType();
        return isCH2EN == 'c' ? CHINESE_TO_ENGLISH : ENGLISH_TO_CHINESE;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        MeHolder meHolder = null;
        OtherHolder otherHolder = null;
        int type = getItemViewType(position);
        if (convertView == null) {
            switch (type) {
                case ENGLISH_TO_CHINESE:
                    convertView = View.inflate(mContext, R.layout.item_chat_msg_text_en, null);
                    meHolder = new MeHolder();
                    meHolder.chat_me_tv_content = (TextView) convertView.findViewById(R.id.chat_en_tv_content);
                    meHolder.chat_me_tv_time = (TextView) convertView.findViewById(R.id.chat_en_tv_time);
                    meHolder.chat_me_tv_translated = (TextView) convertView.findViewById(R.id.chat_en_tv_translated);
                    convertView.setTag(meHolder);
                    break;
                case CHINESE_TO_ENGLISH:
                    convertView = View.inflate(mContext, R.layout.item_chat_msg_text_ch, null);
                    otherHolder = new OtherHolder();
                    otherHolder.chat_other_tv_content = (TextView) convertView.findViewById(R.id.chat_ch_tv_content);
                    otherHolder.chat_other_tv_time = (TextView) convertView.findViewById(R.id.chat_ch_tv_time);
                    otherHolder.chat_other_tv_translated = (TextView) convertView.findViewById(R.id.chat_ch_tv_translated);
                    convertView.setTag(otherHolder);
                    break;
            }
        } else {
            switch (type) {
                case ENGLISH_TO_CHINESE:
                    meHolder = (MeHolder) convertView.getTag();
                    break;
                case CHINESE_TO_ENGLISH:
                    otherHolder = (OtherHolder) convertView.getTag();
                    break;
            }
        }
        VoiceInfo voiceItem = chatList.get(position);
        switch (type) {
            case ENGLISH_TO_CHINESE:
                if (voiceItem.getOrgContent() != null)
                    meHolder.chat_me_tv_content.setText(chatList.get(position).getOrgContent().trim());
                meHolder.chat_me_tv_time.setText(TimeUtil.geTime(chatList.get(position).getInsertTime()));
                if (voiceItem.getTranContent() != null)
                    meHolder.chat_me_tv_translated.setText(chatList.get(position).getTranContent().trim());

                meHolder.chat_me_tv_content.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String path = chatList.get(position).getOrgPath();
                        playRecord(path);
                    }
                });

                meHolder.chat_me_tv_translated.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String translated = ((TextView) v).getText().toString();
                        synthesizeContent(translated, false);
                    }
                });
                break;
            case CHINESE_TO_ENGLISH:
                if (voiceItem.getOrgContent() != null)
                    otherHolder.chat_other_tv_content.setText(chatList.get(position).getOrgContent().trim());
                otherHolder.chat_other_tv_time.setText(TimeUtil.geTime(chatList.get(position).getInsertTime()));
                if (voiceItem.getTranContent() != null)
                    otherHolder.chat_other_tv_translated.setText(chatList.get(position).getTranContent().trim());

                otherHolder.chat_other_tv_content.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String path = chatList.get(position).getOrgPath();
                        playRecord(path);
                    }
                });
                otherHolder.chat_other_tv_translated.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        final String translated = ((TextView) v).getText().toString();
                        synthesizeContent(translated, true);
                    }
                });
                break;
        }
        return convertView;
    }

    public static class MeHolder {
        private TextView chat_me_tv_content;
        private TextView chat_me_tv_time;
        private TextView chat_me_tv_translated;
    }

    public static class OtherHolder {
        private TextView chat_other_tv_content;
        private TextView chat_other_tv_time;
        private TextView chat_other_tv_translated;
    }

    /**
     * 播放录音
     */
    private void playRecord(final String path) {
        if (!TextUtils.isEmpty(path)) {
            File file = new File(path);
            if (file.exists()) {
                stopRecordAndPlay();
                mMyBinder.playRecord(path);
            }
        }
    }

    /**
     * 语音合成
     */
    private void synthesizeContent(String content, boolean isCH2EN) {
        if (!TextUtils.isEmpty(content)) {
            stopRecordAndPlay();
            mMyBinder.synthesizeContent(content, isCH2EN);
        }
    }

    /**
     * 停止录音,播放,合成及消息队列
     */
    private void stopRecordAndPlay() {
        mMyBinder.stopAll();
    }
}